
const request = require('../../../../utils/request');
var page = 1, num = 10, type = 'distance';

function loadingData(that,page,num){
    if(that.data.upflag){
        request.HttpRequst(true,'takeout',false,'',{"lng": that.data.longitude, "lat": that.data.latitude,"page":page,"num": num,"type": type},"GET",false,res => {
            //console.log(res.data)
            if(res.data != ''){
                let goodsData = that.data.goods.concat(res.data)
                that.setData({
                    goods: goodsData
                })
            }else{
                that.setData({
                    upflag: false
                })
            }
        })
    }
    
}

Page({
    data: {
        imgUrls: [
            'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
            'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg'
        ],
        list:[0,1],
        icon:['#f63c3c','#3b9af1','#f36949','#7ee5f4','#f3498d','#9ea2f1','#f8b34d','#b3d465'],
        indicatorDots: true,
        autoplay: false,
        circular: false,
        interval: 2000,
        duration: 500,
        goods: [],
        latitude: 0,    //纬度
        longitude: 0,  //经度
        upflag: true,
        menuIndex:1,
        imgUrl:request.imgUrl
    },
    onLoad: function() {
        page =1;
        //外卖数据
        this.setData({
            latitude: wx.getStorageSync('latitude'),    //纬度
            longitude: wx.getStorageSync('longitude')  //经度
        })
        console.log(this.data.latitude)
        request.HttpRequst(true,'takeout',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":page,"num": num,"type": type},"GET",false,res => {
            console.log(res)
            this.setData({
                goods: res.data
            })
        })
        //外卖菜单
        request.HttpRequst(true,'typeData',false,'',{},"GET",false,res => {
            //debugger
            var allArr = new Array();
            let arr1 = [],arr2=[];
            let arr = res.data;
            for(let i=0;i<Math.floor(arr.length/2);i++){
                arr1.push(arr[i])
            }
            for(let i=8;i<arr.length;i++){
                arr2.push(arr[i])
            }
            allArr.push(arr1,arr2)
            console.log(allArr)
            this.setData({
                list: allArr
            })
        })
    },
    onReady: function(){
        wx.setNavigationBarTitle({
            title: '生活配送'
        })
    },
    goShop(e){
        var id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../../shop/shop?id='+id
        })
        // wx.navigateTo({
        //     url: '../../list/advance/shop/shop?id='+id
        // })
    },
    /*下拉获取更多电影数据*/
    onReachBottom:function(){
        var that = this;
        page++;
        loadingData(that,page,num);
    },

    //附近数据处理
    getDistance () {
        page = 1
        type = 'distance'
        request.HttpRequst(true,'takeout',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":page,"num": num,"type": type},"GET",false,res => {                 
            this.setData({
                goods: res.data,
                upflag: true,
                menuIndex:1
            })
        })
    },
    //销量数据处理
    getSales () {
        page = 1
        type = 'sales'
        request.HttpRequst(true,'takeout',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":page,"num": num,"type": type},"GET",false,res => {                 
            this.setData({
                goods: res.data,
                upflag: true,
                menuIndex:2
            })
        })
    },
    //附近数据处理
    getScore () {
        page = 1
        type = 'score'
        request.HttpRequst(true,'takeout',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":page,"num": num,"type": type},"GET",false,res => {                 
            this.setData({
                goods: res.data,
                upflag: true,
                menuIndex:3
            })
        })
    },

    //点击列表
    seeShop(e) {
        let id = e.currentTarget.dataset.id,page = 1;
        request.HttpRequst(true,'takeout',false,'',{"lng": this.data.longitude, "lat": this.data.latitude,"page":page,"num": num,"type": type,'type_id':id},"GET",false,res => { 
            if(res.code == 200){
                if(res.data.length > 0){
                    this.setData({
                        goods: res.data,
                        upflag: true
                    })
                }else{
                    this.setData({
                        goods: [],
                        upflag: true
                    })
                }
            }else{
                wx.showModal({
                    title: '提示',
                    content: res.info,
                    showCancel: false
                })
            }  
        })
    }
})